// EPropertyDateTime.cpp: implementation of the EPropertyDateTime class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "others/EPropertyDateTimeWindow.h"
#include "others/EPropWndEdit.h"
#include "IPropertyHost.h"
#include "EPropertyDateTime.h"

EPropertyDateTime::EPropertyDateTime(IPropertyHost *pHost, const std::wstring &sName, time_t *pProperty, bool bEnabled)
    : EIconTextButtonProperty(pHost, sName, pProperty, bEnabled)
    , m_pPropertyWindow(nullptr)
    , m_pCtrl(nullptr)
{}

EPropertyDateTime::~EPropertyDateTime()
{
    ClosePropertyWindow();
}

void EPropertyDateTime::OnWindowButtonOk()
{
    if(m_pCtrl) {
        m_pCtrl->OnDoPropApply();
    }
}

void EPropertyDateTime::OnWindowButtonCancel()
{
    ClosePropertyWindow();
}

bool EPropertyDateTime::OnCommand(EPropWnd *pParent, WPARAM wParam, LPARAM lParam)
{
    bool bPropertyChanged = false;
    m_pCtrl = pParent;

    if(!m_pPropertyWindow) {
        CPoint point = m_ButtRect.BottomRight();
        pParent->ClientToScreen(&point);
        CRect rc(0, 0, 280, 18);
        rc.OffsetRect(point.x - rc.Width(), point.y);

        m_pPropertyWindow = std::make_unique<EPropertyDateTimeWindow>(this, pParent);
        m_pPropertyWindow->SetTime(GetPropertyValue());
        m_pPropertyWindow->Create(NULL, m_sName.c_str(), WS_VISIBLE, rc, pParent);
    }
    return bPropertyChanged;
}

bool EPropertyDateTime::OnPropApply()
{
    ASSERT(m_bSelected == true);
    ASSERT(m_pEdit != NULL);
    ASSERT(IsEnabled() == true);

    bool bPropertyChanged = false;
    auto oldValue = GetPropertyValue();
    auto newValue = oldValue;

    if(m_pPropertyWindow) {
        newValue = m_pPropertyWindow->GetTime();
        ClosePropertyWindow();
    } else {
        wchar_t c[256];
        ::GetWindowTextW(m_pEdit->GetSafeHwnd(), c, 255);
        CStringW s(c);

        bool bFormatGood = false;

        int yy = 0;
        int mm = 0;
        int dd = 0;
        int h = 0;
        int m = 0;
        int ss = 0;
        if(swscanf_s(s, L"%4d/%2d/%2d %2d:%2d:%2d", &yy, &mm, &dd, &h, &m, &ss) == 6) {
            bFormatGood = true;
        } else if(swscanf_s(s, L"%4d-%2d-%2d %2d:%2d:%2d", &yy, &mm, &dd, &h, &m, &ss) == 6) {
            bFormatGood = true;
        }

        if(bFormatGood) {
            if((1970 <= yy && yy <= 2099) && (1 <= mm && mm <= 12) && (1 <= dd && dd <= 31) && (0 <= h && h <= 24) && (0 <= m && m <= 59) && (0 <= ss && ss <= 59)) {
                newValue = CTime(yy, mm, dd, h, m, ss).GetTime();
            }
        }
    }

    if(oldValue != newValue && m_pHost->PropertyChanging(m_pProperty, &newValue)) {
        *(time_t *)m_pProperty = newValue;
        bPropertyChanged = true;
    }

    return bPropertyChanged;
}

void EPropertyDateTime::OnPropClose()
{
    EIconTextButtonProperty::OnPropClose();
    OnWindowButtonCancel();
}

bool EPropertyDateTime::HasButton()
{
    return true;
}

bool EPropertyDateTime::HasEdit()
{
    return true;
}

std::wstring EPropertyDateTime::GetTextValue()
{
    CTime t(GetPropertyValue());
    CStringW s = t.Format(L"%Y/%m/%d %H:%M:%S");
    return s.GetString();
}

void EPropertyDateTime::ClosePropertyWindow()
{
    if(m_pPropertyWindow) {
        m_pPropertyWindow->DestroyWindow();
        m_pPropertyWindow.reset();
    }
}

time_t EPropertyDateTime::GetPropertyValue()
{
    return *(time_t *)m_pProperty;
}